package fr.ants.view.renderer.pokemon;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import fr.ants.model.Node;
import fr.ants.view.panel.Canvas;
import fr.ants.view.renderer.NodeRenderer;

/**
 * <p>Renders a node (ground) with an image from Pokemon.</p> 
 *
 * @author Vianney DUPOY DE GUITARD
 */
public class NodePokemonRenderer implements NodeRenderer {

	/** The list of the images that represent the ground. */
	private List<Image> imgs = new ArrayList<>();

	public NodePokemonRenderer() {
		// Loads the images.
		try {
			imgs.add(ImageIO.read(getClass().getResourceAsStream("/draw/poke/node_01.png")));
			imgs.add(ImageIO.read(getClass().getResourceAsStream("/draw/poke/node_02.png")));
			imgs.add(ImageIO.read(getClass().getResourceAsStream("/draw/poke/node_03.png")));
			imgs.add(ImageIO.read(getClass().getResourceAsStream("/draw/poke/node_04.png")));
			imgs.add(ImageIO.read(getClass().getResourceAsStream("/draw/poke/node_05.png")));
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Unable to load image.");
		}
	}

	/** {@inheritDoc} */
	public void draw(int x, int y, Graphics2D g, Node node) {
		Image img;
		if (node.getDifficulty() < Node.MAX_DIFFICULTY * 0.25) {
			img = imgs.get(0);
		} else if (node.getDifficulty() < Node.MAX_DIFFICULTY * 0.5) {
			img = imgs.get(1);
		}else if (node.getDifficulty() < Node.MAX_DIFFICULTY * 0.75) {
			img = imgs.get(2);
		}else if (node.getDifficulty() < Node.MAX_DIFFICULTY ) {
			img = imgs.get(3);
		} else {
			img = imgs.get(4);
		}
		g.drawImage(img, x * Canvas.NODE_SIZE, y * Canvas.NODE_SIZE, Canvas.NODE_SIZE, Canvas.NODE_SIZE, null);
		float pheromoneLevel = node.getPheromoneLevel();
		if (pheromoneLevel > 0) {
			int alpha = pheromoneLevel > 255 ? 255 : (int) pheromoneLevel;
			g.setColor(new Color(250, 170, 250, alpha));
			g.fillRect(x * Canvas.NODE_SIZE, y * Canvas.NODE_SIZE, Canvas.NODE_SIZE, Canvas.NODE_SIZE);
		}
	}
}